﻿using adb;
using Masuit.Tools;
using Microsoft.EntityFrameworkCore;
using MQTTnet.Server;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SensorCollection.DataDealwith
{
    public class SignalControl : IDataDealwith
    {
        private MqttServer mqttServer;
        private DbContextOptionsBuilder<DeviceDbContext> optionsBuilder = new DbContextOptionsBuilder<DeviceDbContext>();

        public DbContextOptionsBuilder<MyDbContext> MyOptionsBuilder { get; }

        public SignalControl(MqttServer mqttServer, DbContextOptionsBuilder<DeviceDbContext> optionsBuilde, DbContextOptionsBuilder<MyDbContext> MyOptionsBuilder)
        {
            this.mqttServer = mqttServer;
            this.optionsBuilder = optionsBuilde;
            this.MyOptionsBuilder = MyOptionsBuilder;
        }

        public long keyId => 820110;


        public object JsonToModel(string json, string deviceSN = null)
        {
            return System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(json);
        }
        public async Task ReactData(object obj, string Topic, string deviceSN = null)
        {
            try
            {
                if ("wsr".Equals(deviceSN))
                {
                    Dictionary<string, string> obj1 = obj as Dictionary<string, string>;
                    if (obj1 == null) return;
                    if (Topic.IsNullOrEmpty()) return;
                    if (obj1.ContainsKey("signal-control") == true || obj1.ContainsKey("signal-adjust") == true)
                    {
                        tSignalControlData_WSR ssd = new tSignalControlData_WSR();
                        ssd.Topic = Topic;
                        ssd.addTime = DateTime.Now;
                        ssd.Data = obj1.ContainsKey("signal-control") ? obj1["signal-control"] : obj1["signal-adjust"];
                        using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                        {
                            await db.tSignalControlData_WSR.AddAsync(ssd);
                            await db.SaveChangesAsync();
                        }
                    }
                }
                else if (deviceSN.Equals("YKF2"))
                {
                    Dictionary<string, string> obj1 = obj as Dictionary<string, string>;
                    if (obj1 == null) return;
                    if (Topic.IsNullOrEmpty()) return;
                    if (obj1.ContainsKey("signal-control") == true || obj1.ContainsKey("signal-adjust") == true)
                    {
                        tSignalControlData_YKF2 ssd = new tSignalControlData_YKF2();
                        ssd.Topic = Topic;
                        ssd.addTime = DateTime.Now;
                        ssd.Data = obj1.ContainsKey("signal-control") ? obj1["signal-control"] : obj1["signal-adjust"];
                        using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                        {
                            await db.tSignalControlData_YKF2.AddAsync(ssd);
                            await db.SaveChangesAsync();
                        }
                    }
                }
                else if (deviceSN.Equals("YKF1"))
                {
                    Dictionary<string, string> obj1 = obj as Dictionary<string, string>;
                    if (obj1 == null) return;
                    if (Topic.IsNullOrEmpty()) return;
                    if (obj1.ContainsKey("signal-control") == true || obj1.ContainsKey("signal-adjust") == true)
                    {
                        tSignalControlData_YKF1 ssd = new tSignalControlData_YKF1();
                        ssd.Topic = Topic;
                        ssd.addTime = DateTime.Now;
                        ssd.Data = obj1.ContainsKey("signal-control") ? obj1["signal-control"] : obj1["signal-adjust"];
                        using (var db = new DeviceDbContext(this.optionsBuilder.Options))
                        {
                            await db.tSignalControlData_YKF1.AddAsync(ssd);
                            await db.SaveChangesAsync();
                        }
                    }
                }


            }
            catch (Exception ex)
            {
                Console.WriteLine("tSignalFailData:" + ex.Message);

            }
        }

    }
}
